package com.letoken.platform.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.letoken.platform.pub.annotation.auth.NotAuthorization;
import com.letoken.platform.pub.annotation.auth.Permission;
import com.letoken.platform.pub.exception.ApiServiceException;
import com.letoken.platform.pub.res.ApiResponse;
import com.letoken.platform.repo.admin.dao.AdminFundConfigMapper;
import com.letoken.platform.repo.admin.po.AdminFundConfig;
import com.letoken.platform.repo.admin.req.user.FundDetailReq;
import com.letoken.platform.repo.admin.req.user.FundInfoRq;
import com.letoken.platform.repo.admin.req.user.RankingDetailReq;
import com.letoken.platform.repo.admin.res.mall.MallIssuDetailRes;
import com.letoken.platform.repo.app.dao.AppFundDetailMapper;
import com.letoken.platform.repo.app.dao.AppFundInfoMapper;
import com.letoken.platform.repo.app.dao.AppRankingDetailMapper;
import com.letoken.platform.repo.app.po.AppFundDetail;
import com.letoken.platform.repo.app.po.AppFundInfo;
import com.letoken.platform.repo.app.po.AppRankingDetail;
import com.letoken.platform.repo.app.res.user.AdminBalanceTotalRes;
import com.letoken.platform.repo.mall.po.AppBalanceWallet;
import com.letoken.platform.repo.utlis.PageDataUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/adminFundController")
public class AdminFundController {

    @Resource
    private AppFundInfoMapper appFundInfoMapper;

    @Resource
    private AppFundDetailMapper appFundDetailMapper;

    @Resource
    private AdminFundConfigMapper adminFundConfigMapper;

    @Resource
    private AppRankingDetailMapper appRankingDetailMapper;

    @Permission(uri = "/adminFundController/fundTotal",qx = "/adminFundController/fundTotal")
    @GetMapping("/fundTotal")
    public ApiResponse<Map<String, BigDecimal>> fundTotal() {
        Map<String,BigDecimal> map = new HashMap<>();
        map.put("one",appFundInfoMapper.adminSumAmountHistory());
        map.put("tow", appFundInfoMapper.adminSumAmount());
        map.put("three",appFundInfoMapper.adminSumAmountHistoryByType(1));
        map.put("four",appFundInfoMapper.adminSumAmountByType(1));
        map.put("five", appFundInfoMapper.adminSumAmountHistoryByType(2));
        map.put("six",appFundInfoMapper.adminSumAmountByType(2));
        map.put("seven",appFundInfoMapper.adminSumAmountHistoryByType(3));

        map.put("eight",appFundInfoMapper.adminSumAmountByType(3));
        return ApiResponse.create(map);
    }

    @Permission(uri = "/adminFundController/incomeTotal",qx = "/adminFundController/incomeTotal")
    @GetMapping("/incomeTotal")
    public ApiResponse<Map<String, BigDecimal>> incomeTotal() {
        Map<String,BigDecimal> map = new HashMap<>();
        map.put("one",appFundDetailMapper.yesterdayIncome());
        map.put("tow", appFundDetailMapper.todayIncome());
        map.put("three",appFundDetailMapper.mouthIncome());
        map.put("four",appFundDetailMapper.sumIncome());
        return ApiResponse.create(map);
    }

    Map<Integer, Integer> typeMap = new HashMap<>(){{
        put(1, 20);
        put(2, 21);
        put(3, 22);
        put(6, 27);

    }};

    @Permission(uri = "/adminFundController/fundDetail",qx = "/adminFundController/fundDetail")
    @PostMapping("/fundDetail")
    public ApiResponse<Page<AppFundDetail>> fundDetail(@RequestBody @Validated FundDetailReq req) {
        Page<AppFundDetail> page = new Page<>(req.getPageReq().getPage(), req.getPageReq().getSize());

        req.setType(typeMap.get(req.getType()));
        return ApiResponse.create(PageDataUtil.getPageData(appFundDetailMapper.getFundDetail(page,req)));
    }

    @Permission(uri = "/adminFundController/fundInfoDetail",qx = "/adminFundController/fundInfoDetail")
    @PostMapping("/fundInfoDetail")
    public ApiResponse<Page<AppFundInfo>> fundInfoDetail(@RequestBody @Validated FundInfoRq req) {
        Page<AppFundInfo> page = new Page<>(req.getPageReq().getPage(), req.getPageReq().getSize());
        Page<AppFundInfo> appFundInfoList = appFundInfoMapper.fundInfoDetail(page,req);
        for(AppFundInfo info : appFundInfoList.getRecords()){
            if(info.getExpirationTime().compareTo(LocalDateTime.now())<=0 && info.getType().equals(1)){
                info.setType(3);
            }
        }
        return ApiResponse.create(PageDataUtil.getPageData(appFundInfoList));
    }

    @Permission(uri = "/adminFundController/getFundconfig",qx = "/adminFundController/getFundconfig")
    @GetMapping("/getFundconfig")
    public ApiResponse<List<AdminFundConfig>> getFundconfig() {
       List<AdminFundConfig> list= adminFundConfigMapper.selectList(new QueryWrapper<>());
        for (AdminFundConfig adminFundConfig :list) {

            adminFundConfig.setRate(adminFundConfig.getRate().multiply(new BigDecimal(100)));
        }
        return ApiResponse.create(list);
    }

    @Permission(uri = "/adminFundController/updateFundconfig",qx = "/adminFundController/updateFundconfig")
    @PostMapping("/updateFundconfig")
    public ApiResponse updateFundconfig(@RequestBody @Validated List<AdminFundConfig> adminFundConfigs) {
        for (AdminFundConfig adminFundConfig:adminFundConfigs) {
            if(adminFundConfig.getId().equals(1)){
                if(adminFundConfig.getRate().compareTo(new BigDecimal("0.0666"))<0 || adminFundConfig.getRate().compareTo(new BigDecimal("0.2333"))>0 ){
                    throw new ApiServiceException("填写错误");
                }
            }
            if(adminFundConfig.getId().equals(2)){
                if(adminFundConfig.getRate().compareTo(new BigDecimal("0.1333"))<0 || adminFundConfig.getRate().compareTo(new BigDecimal("0.3333"))>0 ){
                    throw new ApiServiceException("填写错误");
                }
            }
            if(adminFundConfig.getId().equals(3)){
                if(adminFundConfig.getRate().compareTo(new BigDecimal("0.2333"))<0 || adminFundConfig.getRate().compareTo(new BigDecimal("0.5"))>0 ){
                    throw new ApiServiceException("填写错误");
                }
            }
            if(adminFundConfig.getId().equals(4)){
                if(adminFundConfig.getRate().compareTo(new BigDecimal("0.3"))<0 || adminFundConfig.getRate().compareTo(new BigDecimal("0.6"))>0 ){
                    throw new ApiServiceException("填写错误");
                }
            }
            if(adminFundConfig.getId().equals(5)){
                if(adminFundConfig.getRate().compareTo(new BigDecimal("0.4"))<0 || adminFundConfig.getRate().compareTo(new BigDecimal("0.6666"))>0 ){
                    throw new ApiServiceException("填写错误");
                }
            }
            adminFundConfig.setRate(adminFundConfig.getRate().divide(new BigDecimal(100)));
            adminFundConfigMapper.updateById(adminFundConfig);
        }
        return ApiResponse.create();
    }



    @Permission(uri = "/adminFundController/getRankingSum",qx = "/adminFundController/getRankingSum")
    @GetMapping("/getRankingSum")
    public ApiResponse getRankingSum() {
        Map<String,BigDecimal> map = new HashMap<>();
        map.put("day",appRankingDetailMapper.getRankingSum(1));
        map.put("week", appRankingDetailMapper.getRankingSum(2));
        map.put("sumDay",appRankingDetailMapper.getRanking(1));
        map.put("sumWeek", appRankingDetailMapper.getRanking(2));
        return ApiResponse.create(map);
    }

    @Permission(uri = "/adminFundController/getRankingDetail",qx = "/adminFundController/getRankingDetail")
    @PostMapping ("/getRankingDetail")
    public ApiResponse<Page<AppRankingDetail>> getRankingDetail(@RequestBody @Validated RankingDetailReq req) {
        Page<AppRankingDetail> page = new Page<>(req.getPageReq().getPage(), req.getPageReq().getSize());
        return ApiResponse.create(PageDataUtil.getPageData(appRankingDetailMapper.getRankingDetail(page,req)));
    }
}
